package com.invengo.module_check.ui.fragment.rfidsetting;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.afollestad.materialdialogs.MaterialDialog;
import com.invengo.library_rfid.analyze.EPCAnalyzeFactory;
import com.invengo.library_rfid.analyze._EpcRuleTag;
import com.invengo.library_rfid.classify.RfidModelImpl;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.databinding.FragmentRfidSettingBinding;
import com.invengo.module_check.ui.fragment.widget.AnalyzeTestDialog;
import com.invengo.module_check.ui.fragment.widget.SelectShelfDialog;

import java.util.Arrays;

import androidx.annotation.Nullable;

import java8.util.J8Arrays;
import java8.util.stream.Collectors;
import java8.util.stream.StreamSupport;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.KLog;

/**
 * @author buoker
 * @version 1.0.0
 * @date 2023/10/24
 * @className java.lang.String
 */
public class RFIDSettingFragment extends BaseFragment<FragmentRfidSettingBinding, RFIDSettingViewModel> {

    private MaterialDialog powerDialog, ruleDialog, modelDialog,blueDialog;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_rfid_setting;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.powerDialogEvent.observe(this, needShow -> {
            if (powerDialog == null)
                powerDialog = new MaterialDialog.Builder(getActivity())
                        .title("请选择功率")
                        .items(viewModel.powerMeter.get())
                        .itemsCallbackSingleChoice(viewModel.getPowerPosition(), (dialog, view, which, text) -> {
                            dialog.dismiss();
                            viewModel.changePower(text.toString());
                            return false;
                        }).build();
            if (!powerDialog.isShowing()) {
                powerDialog.setSelectedIndex(viewModel.getPowerPosition());
                powerDialog.show();
            }
        });
        viewModel.uc.ruleDialogEvent.observe(this, unused -> {
            int position = EPCAnalyzeFactory.getInstance().getPosition();
            if (ruleDialog == null)
                ruleDialog = new MaterialDialog.Builder(getActivity())
                        .title("解析模式选择")
                        .items(StreamSupport.stream(EPCAnalyzeFactory.getInstance().getRuleList())
                                .map(ruleTag -> ruleTag.getRuleName())
                                .collect(Collectors.toList()))
                        .itemsCallbackSingleChoice(position, (dialog, view, which, text) -> {
                            viewModel.changeRuleModel(which);
                            dialog.dismiss();
                            return true;
                        }).build();
            if (!ruleDialog.isShowing()) {
                ruleDialog.setSelectedIndex(position);
                ruleDialog.show();
            }
        });
        viewModel.uc.blueDialog.observe(this, position -> {
            if (blueDialog == null)
                blueDialog = new MaterialDialog.Builder(getActivity())
                        .title("请选择链接的蓝牙")
                        .items(viewModel.getDeviceList())
                        .itemsCallbackSingleChoice(position, (dialog, view, which, text) -> {
                            viewModel.changeBlueName(text.toString());
                            dialog.dismiss();
                            return true;
                        }).build();
            if (!blueDialog.isShowing()) {
                blueDialog.setSelectedIndex(position);
                blueDialog.show();
            }
        });
        viewModel.uc.rfidModelDialogEvent.observe(this, unused -> {
            int position = RfidModelImpl.getPosition();
            if (modelDialog == null)
                modelDialog = new MaterialDialog.Builder(getActivity())
                        .title("标签模式选择")
                        .items(RfidModelImpl.getModelList())
                        .itemsCallbackSingleChoice(position, (dialog, view, which, text) -> {
                            viewModel.changeRfidModel(which);
                            dialog.dismiss();
                            return true;
                        }).build();
            if (!modelDialog.isShowing()) {
                modelDialog.setSelectedIndex(position);
                modelDialog.show();
            }
        });

        viewModel.uc.analyzeTestEvent.observe(this, integer -> AnalyzeTestDialog.getInstance().showDialog(getActivity()));
    }
}
